package com.cyh.testcustomui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import android.annotation.SuppressLint;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")

public class CycleProgressBar extends View {
    private static final String TAG = "CycleProgressBar";
    private Context m_Context;

    private int mInnerBackground = Color.BLUE;
    private int mOuterBackground = Color.RED;
    private int mRoundWidth = 10;
    private int mProgressTextSize = 15;
    private int mProgressTextColor = Color.RED;

    private Paint mOuterPaint;
    private Paint mInnerPaint;
    private Paint mTextPaint;

    private int mStepMax = 0;
    private int mCurrentStep = 0;

    public CycleProgressBar(Context context) {
        this(context, null);
    }

    public CycleProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CycleProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        m_Context = context;

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CycleProgressBar);
        mInnerBackground = array.getColor(R.styleable.CycleProgressBar_innerBackground, mInnerBackground);
        mOuterBackground = array.getColor(R.styleable.CycleProgressBar_outerBackground, mOuterBackground);
        mRoundWidth = array.getDimensionPixelSize(R.styleable.CycleProgressBar_roundWidth, mRoundWidth);
        mProgressTextSize = array.getDimensionPixelSize(R.styleable.CycleProgressBar_progressTextSize, mProgressTextSize);
        mProgressTextColor = array.getColor(R.styleable.CycleProgressBar_progressTextColor, mProgressTextColor);
        array.recycle();

        mOuterPaint = new Paint();
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setStrokeWidth(mRoundWidth);
        mOuterPaint.setColor(mOuterBackground);
        mOuterPaint.setStyle(Paint.Style.STROKE);
        //mOuterPaint.setStrokeCap(Paint.Cap.ROUND); //线头和线尾为圆弧型

        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mRoundWidth);
        mInnerPaint.setColor(mInnerBackground);
        mInnerPaint.setStyle(Paint.Style.STROKE);
        //mInnerPaint.setStrokeCap(Paint.Cap.ROUND); //线头和线尾为圆弧型

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mProgressTextColor);
        mTextPaint.setTextSize(mProgressTextSize);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //获取宽高模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int width = 0;
        int height = 0;
        if(widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST) {
            width = height = MyUtil.dp2px(m_Context, 40);
        } else {
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);
        }

        setMeasuredDimension(width > height ? height : width, width > height ? height : width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //先画内圆
        int center = getWidth() / 2;
        int redius = center - mRoundWidth;
        canvas.drawCircle(center, center, redius, mInnerPaint);

        //画外圆弧
        RectF rectF = new RectF(mRoundWidth, mRoundWidth, getWidth() - mRoundWidth, getWidth() - mRoundWidth);
        if(mStepMax == 0) {
            return;
        }
        float percent = (float)mCurrentStep / mStepMax;
        canvas.drawArc(rectF, 0, percent * 360, false, mOuterPaint);

        //画文字
        //画文字
        String stepText = mCurrentStep + "";
        Rect textBounds = new Rect();
        mTextPaint.getTextBounds(stepText, 0, stepText.length(), textBounds);
        int dx = getWidth()/2 - textBounds.width()/2;
        //计算基线
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        int baseLine = getHeight()/2 + dy; //中线 + dy
        canvas.drawText(stepText, dx, baseLine, mTextPaint);
    }


    public int getmStepMax() {
        return mStepMax;
    }

    public synchronized void setmStepMax(int mStepMax) {
        this.mStepMax = mStepMax;
    }

    public  int getmCurrentStep() {
        return mCurrentStep;
    }

    public synchronized void setmCurrentStep(int mCurrentStep) {
        this.mCurrentStep = mCurrentStep;
        invalidate();
    }
}
